package vn.com.demand.widget;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import vn.com.demand.util.DpUtils;

public class ItemBrowserCommand {

	private Object[] params;
	private String sql;
	private ResultSetHandler handler;
	
	public ItemBrowserCommand(String sql, Object[] params, ResultSetHandler handler) {
		this.sql = sql;
		this.params = params;
		this.handler = handler;
	}
	
	public Object execute() throws SQLException {
		return execute(handler);
	}
	
	public Object execute(ResultSetHandler handler) throws SQLException {
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		if (null == params || params.length == 0) {
			return runner.query(sql, handler);
		}
		else {
			return runner.query(sql, params, handler);
		}
	}
}